package com.xuecheng.manage_cms.service;

import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.request.QueryPageRequest;
import com.xuecheng.framework.domain.cms.response.CmsPageResult;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.manage_cms.dao.CmsPageRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * Created by Windows on 2018/11/4.
 */
@Service
public class PageService {
    @Autowired
    CmsPageRepository cmsPageRepository;

    public QueryResponseResult findList(int page, int size, QueryPageRequest queryPageRequest) {
        queryPageRequest = null == queryPageRequest ? new QueryPageRequest() : queryPageRequest;
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains());

        CmsPage cmsPage = new CmsPage();
        if (StringUtils.isNotEmpty(queryPageRequest.getSiteId())) {
            cmsPage.setSiteId(queryPageRequest.getSiteId());
        }
        if (StringUtils.isNotEmpty(queryPageRequest.getPageAliase())) {
            cmsPage.setPageAliase(queryPageRequest.getPageAliase());
        }

        page = page <= 1 ? 0 : page - 1;
        size = size <= 0 ? 10 : size;
        Page<CmsPage> all = cmsPageRepository.findAll(Example.of(cmsPage, exampleMatcher), PageRequest.of(page, size));
        QueryResult<CmsPage> queryResult = new QueryResult<>();
        queryResult.setList(all.getContent());
        queryResult.setTotal(all.getTotalElements());
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }

    public CmsPageResult add(CmsPage cmsPage) {
        CmsPage old = cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(), cmsPage.getSiteId(), cmsPage.getPageWebPath());
        if (null == old) {
            cmsPage.setPageId(null);
            CmsPage save = cmsPageRepository.save(cmsPage);
            return new CmsPageResult(CommonCode.SUCCESS, save);
        }
        return new CmsPageResult(CommonCode.FAIL, null);
    }

    public CmsPage findById(String id) {
        Optional<CmsPage> cmsPage = cmsPageRepository.findById(id);
        return cmsPage.isPresent() ? cmsPage.get() : null;
    }

    public CmsPageResult update(String id, CmsPage cmsPage) {
        CmsPage old = this.findById(id);
        if (null == old) {
            return new CmsPageResult(CommonCode.FAIL,old);
        }
        old.setTemplateId(cmsPage.getTemplateId());
        old.setSiteId(cmsPage.getSiteId());
        old.setPageAliase(cmsPage.getPageAliase());
        old.setPageName(cmsPage.getPageName());
        old.setPageWebPath(cmsPage.getPageWebPath());
        old.setPagePhysicalPath(cmsPage.getPagePhysicalPath());
        CmsPage save = cmsPageRepository.save(old);
        return new CmsPageResult(CommonCode.SUCCESS,old);
    }
}
